package com.lanp.springsd.controller;

import com.lanp.springsd.dao.TOrderMapper;
import com.lanp.springsd.dao.orderMapper;
import com.lanp.springsd.model.TOrder;
import com.lanp.springsd.model.TOrderExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("test")
public class TestRest {
    @Autowired
    TOrderMapper tOrderMapper;
    @Autowired
    orderMapper orderMapper;

    /**
     * 单机/多机调用不同db库
     *
     * @param tOrder
     * @return
     */
    @PostMapping("/adda")
    public Object adda(TOrder tOrder) {
        int i = tOrderMapper.insert(tOrder);
        // TODO:
        return i;
    }

    @PostMapping("/addb")
    public Object addb(TOrder tOrder, int size) {
        List<TOrder> list = new ArrayList<>();
        list.add(tOrder);
        for (int i = 1; i <= size; i++) {
            TOrder tOrder1 = new TOrder();
            tOrder1.setOrderId(tOrder.getOrderId() + i);
            tOrder1.setOrderCode(tOrder.getOrderId() + i + "");
            tOrder1.setUserId(tOrder.getUserId());
            list.add(tOrder1);
        }
        int i = tOrderMapper.batchInsert(list);
        // TODO:
        return i;
    }

    @GetMapping("/geta")
    public Object geta(Long orderId) {
        TOrder i = tOrderMapper.selectByPrimaryKey(orderId);
        // TODO:
        return i;
    }

    @GetMapping("/getAll")
    public Object getAll() {
        TOrderExample example = new TOrderExample();
        return tOrderMapper.selectByExample(example);
    }

}
